package org.hxzon.configdesigner.web.pages;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.json.JSONCollection;
import org.apache.tapestry5.util.TextStreamResponse;
import org.hxzon.configdesigner.core.CfgParser;
import org.hxzon.configdesigner.core.CfgValue;
import org.hxzon.configdesigner.web.pages.base.PageBase;
import org.hxzon.configdesigner.web.util.CfgUtil;

public class ImportJson extends PageBase {

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(int indexCode) {
        CfgValue root = CfgUtil.findCfg(indexCode);
        Object o = CfgParser.toJson(root);
        if (o instanceof JSONCollection) {
            String jsonStr = ((JSONCollection) o).toString(false);
            return new TextStreamResponse("text/json", jsonStr);
        } else {
            return new TextStreamResponse("text/json", o.toString());
        }
    }
}
